<template>
	<view class="container">
		<view class="transfer">
			<view>
				输入或扫码设置对方账号
			</view>
			<view class="choose_account">							
				<view>
					<input type="text" placeholder="对方钱包账号" v-model="targetCustomerNumber" class="targetCustomerNumber">
				</view>	
				<view @click="scanQrcode">
					<image src="../../static/icon/saoma2.png"></image>
				</view>
			</view>
			<view>
				<input type="number" placeholder="输入转账金额" class="transferAmount" v-model="transferAmount">
			</view>
		</view>
		<view>
			<button type="primary" @click="transfer">立即转账</button>
		</view>
	</view>
</template>

<script>
	import Global from '../../common/global.js'
	
	export default {
		data() {
			return {
				customerNumber:'',	
				targetCustomerNumber:'',
				transferAmount:'',
				result:''
			}
		},
		methods: {					
			//转账操作
			transfer(){
				
				if(this.customerNumber == this.targetCustomerNumber) {
					uni.showToast({
						title:'无法给自己转账',
						icon:'none'
					})
					
					return
				}				
				
				uni.showModal({
					title: '转账提示',
					content: '确认向账户' + this.targetCustomerNumber + '转账' + this.transferAmount + '?' ,
					success: (res)=> {
						if (res.confirm) {
							
							new Promise((resolve, reject) => {
								//先进行客户校验
								uni.request({
									url: Global.BASE_URL + '/ewallet/queryCustomer',
									header:{
										'content-type': 'application/x-www-form-urlencoded'
									},
									data:{
										customerNumber:this.targetCustomerNumber										
									},
									method:'POST',
									success:(res)=>{ //注意使用箭头函数						
										if (res.statusCode == 200 && res.data) {
											resolve(res);
										} else {
											reject("目标用户不存在");
										}
									},
									fail(err) {
										uni.showToast({
											title:'查询客户接口调用失败',
											icon:'none'
										})
									}
								})
							})
							.then(res => {					
								
								let p = new Promise((resolve, reject) => {
									
									//进行当前钱包余额校验
									uni.request({
										url: Global.BASE_URL + '/ewallet/queryCustomer',
										header:{
											'content-type': 'application/x-www-form-urlencoded'
										},
										data:{
											customerNumber:this.customerNumber										
										},
										method:'POST',
										success:(res)=>{ //注意使用箭头函数						
											if (parseFloat(res.data.balance) >= parseFloat(this.transferAmount)) {
												resolve(res);
											} else {
												reject("钱包余额不足，请先充值");
											}
										},
										fail(err) {
											uni.showToast({
												title:'查询客户接口调用失败',
												icon:'none'
											})
										}
									})	
								})
								
								return p								
							})
							.then(res => {									
								//进行钱包转账
								uni.request({
									url: Global.BASE_URL + '/ewallet/transferByWallet',
									header:{
										'content-type': 'application/x-www-form-urlencoded'
									},
									data:{
										customerNumber:this.customerNumber,
										targetCustomerNumber:this.targetCustomerNumber,
										transferAmount:this.transferAmount
									},
									method:'POST',
									success:(res)=>{ //注意使用箭头函数						
										uni.showToast({
											title:'转账成功',
											duration: 1000
										})
										
										setTimeout(()=>{
											uni.switchTab({
												url:'../home/home'
											})
										},1000);
									},
									fail(err) {
										uni.showToast({
											title:'钱包转账接口调用失败',
											icon:'none'
										})
									}
								})							
							})
							.catch(err => {
								uni.showToast({
									title:err,
									icon:'none'
								})
							}) 							
						} 
					}
				});					
			},
			//扫描二维码
			scanQrcode(){
				uni.scanCode({				   
				    success: (res)=> {							
						this.result = res.result
						
						//解析json串
						let json = JSON.parse(res.result)
						
						//如果成功读取商户信息 
						if (json && json.targetCustomerNumber) {
							this.targetCustomerNumber = json.targetCustomerNumber;							
						} else {
							this.targetCustomerNumber = '';
														
							uni.showToast({
								title:'未读取到有效用户账号数据',
								icon:'none'
							})
						}
				    },
					fail:(err)=> {						
						this.targetCustomerNumber = '';
												
						uni.showToast({
							title:'未读取到有效用户账号数据',
							icon:'none'
						})
					}
				});
			}
		},
		onLoad() {
			//读取出客户编号
			this.customerNumber = uni.getStorageSync('customerNumber')			
		}
	}
</script>

<style>
	.container {		
		padding: 15rpx;
	}
	
	.transfer {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.targetCustomerNumber {
		width: 500rpx;
		height: 100rpx;
		font-size: 48rpx;
		border-bottom: 1px solid #C0C0C0
	}
	
	.choose_account {
		font-size: 40rpx;
		display: flex;
		justify-content:space-around;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}

	.choose_account image {
		width: 120rpx;
		height: 120rpx;
	}
	
	.transferAmount {
		width: 600rpx;
		height: 150rpx;
		color: #333333;
		font-size: 48rpx;
		border-bottom: 1px solid #C0C0C0;
		margin-bottom: 50rpx;
	}
</style>